package com.hjx.windowtool.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WinUtil {
	 static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static ChromeDriver chromeDriver;
	public static void loginMi(String username,String password) {
		ChromeDriver d = getChromeDriver();
		d.get("https://account.xiaomi.com/pass/serviceLogin");
		WebElement userNameElement = d.findElement(By.id("username"));
		if(userNameElement==null) {
			return;
		}
		userNameElement.sendKeys(username);
		d.findElement(By.id("pwd")).sendKeys(password);
		d.findElement(By.id("login-button")).click();
	}
	public static Set<Cookie> getCookies(){
		Set<Cookie> cookies =chromeDriver.manage().getCookies();
		for (Cookie ck : cookies) {
			System.out.println(ck.getName() + ";" + ck.getValue() + ";"
					+ ck.getDomain() + ";" + ck.getPath() + ";"
					+ formatDate(ck.getExpiry()) + ";" + ck.isSecure());
		}
		return cookies;
	}
	public static String formatDate(Date date) {
		try {
			return sdf.format(date);
		} catch (Exception e) {
			
		}
		return null;
	}
	public static Date parseDate(String s) {
		try {
			if(s==null||s.trim().equals("")||s.trim().equals("null")) {
				return null;
			}
			return sdf.parse(s);
		} catch (Exception e) {
		}
		return null;
	}
	public  static ChromeDriver getChromeDriver() {
		if (chromeDriver==null) {
			System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
			chromeDriver = new ChromeDriver();
		}
		return chromeDriver;
	}
}
